/**
 * 
 */
package com.wubo.sec.web.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.wubo.common.util.StringUtils;
import com.wubo.sec.bean.UserGroupRelate;
import com.wubo.sec.service.IUserGroupService;

import flexjson.JSONSerializer;

/**
 * @author wubo
 * @CreateDate 2010-8-9
 * @version 1.0.01
 */
@Controller
public class UserGroupMgtAction extends BaseSecAction {
	@Autowired
	private IUserGroupService userGroupService;
	
	private String userCode;
	
	private String groupCode;

	public IUserGroupService getUserGroupService() {
		return userGroupService;
	}

	public void setUserGroupService(IUserGroupService userGroupService) {
		this.userGroupService = userGroupService;
	}

	public String getUserCode() {
		return userCode;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	public String getGroupCode() {
		return groupCode;
	}

	public void setGroupCode(String groupCode) {
		this.groupCode = groupCode;
	}
	
	public String show(){
		try{
			groupCode = userGroupService.getGroupCode(userCode);
			
			logger.debug("Find user's group .UserCode:"+userCode+", GroupCode:"+groupCode);
			
			jsonSuccess(new JSONSerializer().serialize(groupCode), true);
		}catch(RuntimeException t){
			jsonError(null);
			throw t;
		}
		return null;
	}
	
	public String relate(){
		logger.debug("relate group to user .UserCode:"+userCode+", GroupCode:"+groupCode);

		try{
			if(StringUtils.isEmpty(groupCode)){
				groupCode = null;
			}
			userGroupService.relate( new UserGroupRelate(userCode, groupCode) );
			
			jsonSuccess(null);
		}catch(RuntimeException t){
			jsonError(null);
			throw t;
		}
		return null;
	}
	
}
